import json
from .collections import create_collections

class Createitems(create_collections):
    def __init__(self,response,client_host,client_port,collections_id,limit,tokenstatus,offsetco,count):
        super(Createitems,self).__init__(response,client_host,client_port)
        self.limit=limit
        self.collections_id=collections_id
        self.tokenstatus=tokenstatus
        self.offsetco=offsetco
        self.count=count
    def create_items(self):
        collections_list=json.loads(self.response["json_encoded_response"])["column_1"]
       
        returned=len(collections_list)
        features=[]

     
        for collection in collections_list:
                    
                    
                    itemccs=json.loads(collection)
                    
                    id=itemccs["id"]
                    itemccs["id"]=id
                    itemccs["collection"] = self.collections_id
                    itemccs["type"] = "Feature"
                    itemccs["stac_version"]="1.0.0"
                    links=[
                {
                    "rel": "collection",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}"
                },
                {
                    "rel": "parent",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}"
                },
                {
                    "rel": "root",
                    "type": "application/json",
                    "href": f"http://{self.host}:{self.port}/"
                },
                {
                    "rel": "self",
                    "type": "application/geo+json",
                    "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items/{id}"
                }
            ] 
                    itemccs["links"]=links
                    features.append(itemccs)
      
        part1={
        "type": "FeatureCollection",
        "context": {
            "limit": self.limit,
            "returned": returned
                    }
        }
        part1["features"]=features
        

        links1=[
        {
            "rel": "items",
            "type": "application/geo+json",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items"
        },
        {
            "rel": "parent",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/"
        },
        {
            "rel": "root",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/"
        },
        {
            "rel": "self",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}"
        }]

       
        if self.tokenstatus==0:
           
            token_next={
                "rel": "next",
                "type": "application/geo+json",
                "method": "GET",
                "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items?token=next:{self.limit}"
                        }
            links1.append(token_next)
       
        elif self.tokenstatus==1:
                
                
            id_next=features[-1]["id"]
            id_pev=features[0]["id"]
           
            if self.offsetco+self.limit<self.count:
                    
                token_next={
                "rel": "next",
                "type": "application/geo+json",
                "method": "GET",
                "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items?token=next:{self.offsetco+self.limit}"
                } 
                links1.append(token_next) 
            if self.offsetco-self.limit>=0:
                    
                token_pev={
            "rel": "previous",
            "type": "application/geo+json",
            "method": "GET",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items?token=prev:{self.offsetco-self.limit}"
            }
                links1.append(token_pev) 
           
        
        part1["links"]=links1
        return part1
    

    def create_itemid(self):
        collections_list=json.loads(self.response["json_encoded_response"])["column_1"]
        part1=json.loads(collections_list[0])
        id=part1.get("id")
        part1["id"]=id
        part1["collection"] = self.collections_id
        part1["type"] = "Feature"
        part1["stac_version"]="1.0.0"
        links =[
        {
            "rel": "collection",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}"
        },
        {
            "rel": "parent",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}"
        },
        {
            "rel": "root",
            "type": "application/json",
            "href": f"http://{self.host}:{self.port}/"
        },
        {
            "rel": "self",
            "type": "application/geo+json",
            "href": f"http://{self.host}:{self.port}/collections/{self.collections_id}/items/{id}"
        }
        ]    
        part1["links"]=links

        return part1